package dper.study.codes.web.controller;

import dper.study.codes.activemq.QueueConstants;
import dper.study.codes.activemq.QueueSender;
import dper.study.codes.activemq.TopicSender;
import dper.study.codes.elasticsearch.model.GitRepoModel;
import dper.study.codes.web.common.response.WebResponse;
import dper.study.codes.web.common.response.WebResponseCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by leichen on 2017/3/23.
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {
    Logger logger = Logger.getLogger(TestController.class);

    @Autowired
    private QueueSender queueSender ;

    @Autowired
    private TopicSender topicSender ;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/index",method= RequestMethod.GET)
    @ResponseBody
    public WebResponse index(){
        WebResponse ret = new WebResponse() ;
        ret.setSuccess(true);
        ret.setResponseCode(WebResponseCode.WRC_OK);
        ret.setCodeMessage("/test/index return normally");
        ret.setData("index");
        return ret;
    }

    @RequestMapping(value = "/index_mav",method= RequestMethod.GET)
    public ModelAndView userIndex(){

        queueSender.send(QueueConstants.FIRSTTEST_QUEUE,request.toString());
        queueSender.send(QueueConstants.FIRSTTEST_QUEUE,request.getRequestURI());
        topicSender.send(QueueConstants.FIRSTTEST_QUEUE,request.getRequestURI());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("test/index_mav");
        modelAndView.addObject("username","chenlei") ;
        modelAndView.addObject("nowtime",new Date()) ;

        List<GitRepoModel> gitRepoModelList = new ArrayList<GitRepoModel>() ;
        GitRepoModel gitRepoModel1 = new GitRepoModel() ;
        gitRepoModel1.setRepoName("dper-study-common");
        gitRepoModel1.setAuthorId(1);
        gitRepoModelList.add(gitRepoModel1) ;
        GitRepoModel gitRepoModel2 = new GitRepoModel() ;
        gitRepoModel2.setRepoName("dper-study-concurrent");
        gitRepoModel2.setAuthorId(1);
        gitRepoModelList.add(gitRepoModel2) ;
        GitRepoModel gitRepoModel3 = new GitRepoModel() ;
        gitRepoModel3.setRepoName("dper-study-elasticsearch");
        gitRepoModel3.setAuthorId(1);
        gitRepoModelList.add(gitRepoModel3) ;

        modelAndView.addObject("gitRepoList",gitRepoModelList) ;
        return modelAndView ;
    }
}
